//
//  File.swift
//  
//
//  Created by CSS on 8/17/23.
//

import Foundation
import UIKit

class HomeCollectionFlowlayout: UICollectionViewFlowLayout {
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        guard let attributes = super.layoutAttributesForElements(in: rect) else {
            return nil
        }
        var newAttributes: [UICollectionViewLayoutAttributes] = []
        
        let fromX = sectionInset.left
        var attributeFrame: CGRect?
        
        for attribute in attributes {
            if attributeFrame == nil || attributeFrame!.minY != attribute.frame.minY {
                //第一个或者换行了
                attributeFrame = attribute.frame
                attributeFrame?.origin.x = fromX
            }
            attributeFrame?.size = attribute.frame.size
            let newAttribute = attribute
            newAttribute.frame = attributeFrame!
            attributeFrame?.origin.x = newAttribute.frame.maxX + minimumInteritemSpacing
            newAttributes.append(newAttribute)
        }
        
        return newAttributes
    }
    
}
